// ChatBaseDoc.cpp : implementation file
//

#include "stdafx.h"
#include "Chat.h"
#include "ChatBaseDoc.h"


// CChatBaseDoc

IMPLEMENT_DYNCREATE(CChatBaseDoc, CDocument)

CChatBaseDoc::CChatBaseDoc()
: m_pPeerInterface(NULL)
{
}

BOOL CChatBaseDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	m_pPeerInterface = RakNetworkFactory::GetRakPeerInterface();

	return TRUE;
}

CChatBaseDoc::~CChatBaseDoc()
{
	if (m_pPeerInterface)
	{
		m_pPeerInterface->Shutdown(300);
		RakNetworkFactory::DestroyRakPeerInterface(m_pPeerInterface);
		m_pPeerInterface = NULL;
	}
}


BEGIN_MESSAGE_MAP(CChatBaseDoc, CDocument)
END_MESSAGE_MAP()


// CChatBaseDoc diagnostics

#ifdef _DEBUG
void CChatBaseDoc::AssertValid() const
{
	CDocument::AssertValid();
}

#ifndef _WIN32_WCE
void CChatBaseDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif
#endif //_DEBUG

#ifndef _WIN32_WCE
// CChatBaseDoc serialization

void CChatBaseDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}
#endif


// CChatBaseDoc commands

bool CChatBaseDoc::Connect( const char* host, unsigned short remotePort )
{
	return m_pPeerInterface->Connect(host, remotePort, NULL,0);
}

Packet* CChatBaseDoc::Receive( void )
{
	return m_pPeerInterface->Receive();
}

void CChatBaseDoc::DeallocatePacket( Packet *packet )
{

	m_pPeerInterface->DeallocatePacket(packet);
}

uint32_t CChatBaseDoc::Send( const RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceipt/*=0 */ )
{
	return m_pPeerInterface->Send(bitStream, priority, reliability, orderingChannel, systemIdentifier, broadcast, forceReceipt);
}

uint32_t CChatBaseDoc::Send( const char *data, const int length, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceipt/*=0 */ )
{
	return m_pPeerInterface->Send(data, length, priority, reliability, orderingChannel, systemIdentifier, broadcast, forceReceipt);
}

bool CChatBaseDoc::StartUpRaknet( unsigned short maxConnections, int _threadSleepTimer, SocketDescriptor *socketDescriptors, unsigned socketDescriptorCount )
{
	
	bool result = m_pPeerInterface->Startup(maxConnections, _threadSleepTimer, socketDescriptors, socketDescriptorCount);
	m_pPeerInterface->SetMaximumIncomingConnections(MAX_CHAT_CLIENTS);

	return result;
}

void CChatBaseDoc::SetNetworkIDManager( NetworkIDManager *manager )
{
	m_pPeerInterface->SetNetworkIDManager(manager);
}

void CChatBaseDoc::AttachRaknetPlugin( PluginInterface2 *plugin )
{
	m_pPeerInterface->AttachPlugin(plugin);
}

void CChatBaseDoc::DetachRaknetPlugin( PluginInterface2 *plugin )
{
	m_pPeerInterface->DetachPlugin(plugin);
}


void CChatBaseDoc::CloseConnection( const SystemAddress& target )
{
	m_pPeerInterface->CloseConnection(target, true);
}

const RakNetGUID& CChatBaseDoc::GetGuidFromSystemAddress( const SystemAddress& input ) const
{
	return m_pPeerInterface->GetGuidFromSystemAddress(input);
}

SystemAddress CChatBaseDoc::GetSystemAddressFromGuid( const RakNetGUID& input ) const
{
	return m_pPeerInterface->GetSystemAddressFromGuid(input);
}